import BasePresenter from "../../../../../../redare/presenter/BasePresenter";
import * as PlatformHttp from "../../../http/component/core/PlatformHttp";

/**
 * Created by RdevPlugins.JsPresenterBuilderAction on 2020-03-07 13:02:40.
 */

export default class PlatformPresenter extends BasePresenter {

    /**
     * @param body: {key, platform, id, enabled, incDel}
     * @param query: {curPage,pageSize}
     * @returns Promise
     */
    findPlatformPage(body, query, success, fails) {

        if (this._onBeforeFindPlatformPage) {
            let data = this._onBeforeFindPlatformPage(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PlatformHttp.findPlatformPage(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeFindPlatformPageSuccess) {
                    result = this._onBeforeFindPlatformPageSuccess(result, {body, query});
                }

                if (this.view && this.view._onFindPlatformPageSuccess) {
                    this.view._onFindPlatformPageSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {key, platform, id, enabled, incDel}
     * @param query: {returnAll,curPage,pageSize}
     * @returns Promise
     */
    findPlatformList(body, query, success, fails) {

        if (this._onBeforeFindPlatformList) {
            let data = this._onBeforeFindPlatformList(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PlatformHttp.findPlatformList(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeFindPlatformListSuccess) {
                    result = this._onBeforeFindPlatformListSuccess(result, {body, query});
                }

                if (this.view && this.view._onFindPlatformListSuccess) {
                    this.view._onFindPlatformListSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {id, platform, enabled, incDel}
     * @param query: {}
     * @returns Promise
     */
    getPlatform(body, query, success, fails) {

        if (this._onBeforeGetPlatform) {
            let data = this._onBeforeGetPlatform(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PlatformHttp.getPlatform(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeGetPlatformSuccess) {
                    result = this._onBeforeGetPlatformSuccess(result, {body, query});
                }

                if (this.view && this.view._onGetPlatformSuccess) {
                    this.view._onGetPlatformSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {id, platform, key, defaults, enabled}
     * @param query: {curPage,pageSize}
     * @returns Promise
     */
    findMenuPage(body, query, success, fails) {

        if (this._onBeforeFindMenuPage) {
            let data = this._onBeforeFindMenuPage(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PlatformHttp.findMenuPage(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeFindMenuPageSuccess) {
                    result = this._onBeforeFindMenuPageSuccess(result, {body, query});
                }

                if (this.view && this.view._onFindMenuPageSuccess) {
                    this.view._onFindMenuPageSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {id, platform, key, defaults, enabled}
     * @param query: {returnAll,curPage,pageSize}
     * @returns Promise
     */
    findMenuList(body, query, success, fails) {

        if (this._onBeforeFindMenuList) {
            let data = this._onBeforeFindMenuList(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PlatformHttp.findMenuList(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeFindMenuListSuccess) {
                    result = this._onBeforeFindMenuListSuccess(result, {body, query});
                }

                if (this.view && this.view._onFindMenuListSuccess) {
                    this.view._onFindMenuListSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {id, platform, enabled}
     * @param query: {}
     * @returns Promise
     */
    getMenu(body, query, success, fails) {

        if (this._onBeforeGetMenu) {
            let data = this._onBeforeGetMenu(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PlatformHttp.getMenu(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeGetMenuSuccess) {
                    result = this._onBeforeGetMenuSuccess(result, {body, query});
                }

                if (this.view && this.view._onGetMenuSuccess) {
                    this.view._onGetMenuSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {id, platform, userPlatform, type, key, code, admin, defaults, platformUnionUserPlatform}
     * @param query: {curPage,pageSize}
     * @returns Promise
     */
    findRolePage(body, query, success, fails) {

        if (this._onBeforeFindRolePage) {
            let data = this._onBeforeFindRolePage(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PlatformHttp.findRolePage(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeFindRolePageSuccess) {
                    result = this._onBeforeFindRolePageSuccess(result, {body, query});
                }

                if (this.view && this.view._onFindRolePageSuccess) {
                    this.view._onFindRolePageSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {id, platform, userPlatform, type, key, code, admin, defaults, platformUnionUserPlatform}
     * @param query: {returnAll,curPage,pageSize}
     * @returns Promise
     */
    findRoleList(body, query, success, fails) {

        if (this._onBeforeFindRoleList) {
            let data = this._onBeforeFindRoleList(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PlatformHttp.findRoleList(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeFindRoleListSuccess) {
                    result = this._onBeforeFindRoleListSuccess(result, {body, query});
                }

                if (this.view && this.view._onFindRoleListSuccess) {
                    this.view._onFindRoleListSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {id, platform, type, userPlatform, code}
     * @param query: {}
     * @returns Promise
     */
    getRole(body, query, success, fails) {

        if (this._onBeforeGetRole) {
            let data = this._onBeforeGetRole(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PlatformHttp.getRole(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeGetRoleSuccess) {
                    result = this._onBeforeGetRoleSuccess(result, {body, query});
                }

                if (this.view && this.view._onGetRoleSuccess) {
                    this.view._onGetRoleSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {platform, userPlatform, name, code, des}
     * @param query: {}
     * @returns Promise
     */
    addRole(body, query, success, fails) {

        if (this._onBeforeAddRole) {
            let data = this._onBeforeAddRole(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PlatformHttp.addRole(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeAddRoleSuccess) {
                    result = this._onBeforeAddRoleSuccess(result, {body, query});
                }

                if (this.view && this.view._onAddRoleSuccess) {
                    this.view._onAddRoleSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {id, platform, userPlatform, name, code, des}
     * @param query: {}
     * @returns Promise
     */
    modifyRole(body, query, success, fails) {

        if (this._onBeforeModifyRole) {
            let data = this._onBeforeModifyRole(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PlatformHttp.modifyRole(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeModifyRoleSuccess) {
                    result = this._onBeforeModifyRoleSuccess(result, {body, query});
                }

                if (this.view && this.view._onModifyRoleSuccess) {
                    this.view._onModifyRoleSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

    /**
     * @param body: {id, platform, userPlatform}
     * @param query: {}
     * @returns Promise
     */
    delRole(body, query, success, fails) {

        if (this._onBeforeDelRole) {
            let data = this._onBeforeDelRole(body, query);
            if (!data || typeof data !== 'object') {
                return;
            }
            body = data.body;
            query = data.query;
        }

        PlatformHttp.delRole(body, query).then(
            result => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();

                if (success) {
                    success(result);
                    return;
                }

                if (this._onBeforeDelRoleSuccess) {
                    result = this._onBeforeDelRoleSuccess(result, {body, query});
                }

                if (this.view && this.view._onDelRoleSuccess) {
                    this.view._onDelRoleSuccess(result, {body, query});
                }
            },
            error => {
                this.closeDoingIndicator();
                this.closeLoadingIndicator();
                this.closeLoadMoreIndicator();
                this.tipError(error);

                if (fails) {
                    fails(error);
                }
            }
        );
    }

}
